package com.thememaker.lockstyle;

import com.thememake.entity.ThemeFileInfo;
import com.thememaker.lockstyle.bean.ui.*;
import com.thememaker.lockstyle.control.*;
import com.thememaker.lockstyle.parsii.ExpressionUtils;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

public class LockstyleControlUtils {
    private ScrollPane leftAnchorPane;
    private ThemeFileInfo themeFileInfo;
    private Map<String, BaseView> baseViewMap;
    private Map<String, ILockscreenControl> iLockscreenControlMap;
    private ExpressionUtils expressionUtils;
    private LockstyleShowUtils lockstyleShowUtils;


    public LockstyleControlUtils(ScrollPane leftAnchorPane, ThemeFileInfo themeFileInfo, ExpressionUtils expressionUtils, LockstyleShowUtils lockstyleShowUtils) {
        this.leftAnchorPane = leftAnchorPane;
        this.themeFileInfo = themeFileInfo;
        this.expressionUtils = expressionUtils;
        baseViewMap = new HashMap<>();
        iLockscreenControlMap = new HashMap<>();
        this.lockstyleShowUtils = lockstyleShowUtils;
        leftAnchorPane.setPadding(new Insets(10, 10, 10, 10));
    }

    public void showControl(BaseView baseView, Consumer<BaseView> consumer) {
        Node node = Optional.ofNullable(iLockscreenControlMap.get(baseView.getId()))
                .flatMap(iLockscreenControl -> Optional.ofNullable(iLockscreenControl.getCacheLockscreenControl()))
                .orElseGet(() -> Optional.ofNullable(initILockscreenControl(baseView)).map(iLockscreenControl -> {
                    if (iLockscreenControl instanceof WallpaperControl){
                        ((WallpaperControl) iLockscreenControl).setLockstyleShowUtils(lockstyleShowUtils);
                    }
                    iLockscreenControlMap.put(baseView.getId(), iLockscreenControl);
                    return iLockscreenControl.initLockscreenControl(
                            baseView, Paths.get(themeFileInfo.getOutputDir().getParent(), "lockstyle", "lockscreen"), expressionUtils, consumer);
                }).orElse(null));
        leftAnchorPane.setContent(node);
    }

    public void showControl(String controlName, Consumer<BaseView> consumer) {
        if (WallpaperControl.class.getSimpleName().equals(controlName)) {
            System.err.println("WallpaperControl ");
            WallpaperControl wallpaperControl =  new WallpaperControl();
            wallpaperControl.setLockstyleShowUtils(lockstyleShowUtils);
            leftAnchorPane.setContent(wallpaperControl.initLockscreenControl(null,
                    Paths.get(themeFileInfo.getOutputDir().getParent(), "lockstyle", "lockscreen"), expressionUtils, consumer));
        }

        if (TimeGroupControl.class.getSimpleName().equals(controlName)) {
            System.err.println("TimeGroupControl ");
            leftAnchorPane.setContent(new TimeGroupControl().initLockscreenControl(null,
                    Paths.get(themeFileInfo.getOutputDir().getParent(), "lockstyle", "lockscreen"), expressionUtils, consumer));
        }

        if (UnlockerControl.class.getSimpleName().equals(controlName)) {
            System.err.println("UnlockerControl ");
            leftAnchorPane.setContent(new UnlockerControl(lockstyleShowUtils).initLockscreenControl(null,
                    Paths.get(themeFileInfo.getOutputDir().getParent(), "lockstyle", "lockscreen"), expressionUtils, consumer));
        }

    }

    private ILockscreenControl initILockscreenControl(BaseView baseView) {
        if (baseView instanceof Image) {
            if ("bg".equals(baseView.getName())) {
                return new WallpaperControl();
            } else {
                return new ImageControl();
            }
        }
        if (baseView instanceof Wallpaper) {
            return new WallpaperControl();
        }
        if (baseView instanceof Unlocker) {
            return new UnlockerControl(lockstyleShowUtils);
        }
        if (baseView instanceof Group) {
            if ("time".equals(baseView.getName()) || "time_grp".equals(baseView.getName())) {
                return new TimeGroupControl();
            }
        }
        return null;
    }

    public void clear(){
        Optional.ofNullable(iLockscreenControlMap).ifPresent(Map::clear);
    }

    public void update() {
        if (iLockscreenControlMap != null) {
            iLockscreenControlMap.keySet().forEach(key -> {
                iLockscreenControlMap.get(key).updateView();
            });
        }
    }

}
